import StorageHelper from "../../engine/StorageHelper";


export default abstract class BaseStorage {
    //存档的值
    protected value: any;
    //存档的key
    protected key: string;
    //初始值
    protected initValue: any;
    //是否有数据
    protected dataFlag: boolean = true;

    constructor(key, initValue) {
        this.key = key;
        this.initValue = initValue;

        this.dataFlag = this.loadValue();
    }
    /**
     * 是否已经有存档数据
     */
    isHaveData() {
        return this.dataFlag;
    }

    static getLength(){
        return StorageHelper.getLength();
    }

    protected abstract loadValue(): boolean;

    //保存
    protected saveValue() {
        StorageHelper.setJsonBase64(this.key, this.value);
    }

    //获取数据
    protected getStorage() {
        return StorageHelper.getJsonBase64(this.key)
    }
    //获取值
    getValue() {
        return this.value;
    }
    //设置值
    setValue(value, save: boolean = true) {
        if (this.value != value) {
            this.value = value;
            if (save) {
                this.saveValue();
            }
        }

    }

}